package com.draw.cli.gui.panel.home;

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.gui.panel.UserTable;
import com.draw.cli.webclient.DrawingClient;
import com.draw.core.Drawing;
import com.draw.core.User;
import com.draw.webinterface.DrawingInterface;

public class SentDrawPanel extends JPanel implements Refreshable, Runnable {
	private static final long serialVersionUID = 6015939217880763417L;

	private static SentDrawPanel instance = new SentDrawPanel();

	public static SentDrawPanel getInstance() {
		return instance;
	}

//	private DefaultListModel<User> model;
//	private JList<User> idents;

	private UserTable table;

	public SentDrawPanel() {
		this.setBorder(BorderFactory.createTitledBorder("Dessins envoyés"));
		this.setLayout(new BorderLayout());
		this.table = new UserTable();
		this.add(this.table);
//		this.model = new DefaultListModel<User>();
//		this.idents = new JList<User>(this.model);
//		JScrollPane scroll = new JScrollPane(this.idents);
//		this.add(scroll);
	}

	@Override
	public void refresh() {
		new Thread(this).start();
	}

	@Override
	public void run() {
		ArrayList<User> users = new ArrayList<User>();
		this.table.setUsers(users);
		users = new ArrayList<User>();
//		this.model.clear();
		User user = ConfigManager.getInstance().getCurrentUser();
		if ( user != null) {
			try {
				DrawingInterface di = DrawingClient.getInstance();
				List<Drawing> ld = di.getDrawingSent(user);
				for (Drawing draw : ld) {
					//this.model.addElement(draw.getDestination());
					users.add(draw.getDestination());
				}
			} catch (MalformedURLException | WebServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.table.setUsers(users);
	}
}
